package com.paradise.ytj.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.paradise.ytj.R;
import com.paradise.ytj.activity.DeveloperActivity;
import com.paradise.ytj.activity.MainActivity;
import com.paradise.ytj.bean.Constant;
import com.paradise.ytj.bean.QuitEvent;
import com.paradise.ytj.utils.RestartAPPTool;
import com.paradise.ytj.utils.SpUtils;
import com.paradise.ytj.view.ChoiceItem;
import com.paradise.ytj.view.CustomDialog;
import com.paradise.ytj.view.ItemSelectContainer;
import com.paradise.api.FaceLogin;
import com.paradise.api.FacePhone;
import com.paradise.api.FacephonePreferences;

import org.greenrobot.eventbus.EventBus;
import org.linphone.core.LinphoneAddress;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class SettingFragment extends Fragment implements View.OnClickListener, View.OnFocusChangeListener {

    private View inflate;
    private ChoiceItem ciAccount;
    private ChoiceItem ciDomain;
    private ChoiceItem ciPassword;
    private ChoiceItem ciProxy;
    private ChoiceItem ciName;
    private ChoiceItem ciLanguage;
    private ItemSelectContainer tvQuit;
    private ItemSelectContainer tvLogin;
    private ChoiceItem ciAnswer;
    private ChoiceItem ciUpdate;
    private ChoiceItem ciLaunch;
    private ChoiceItem ciFrame;
    private ChoiceItem ciStream;
    private ChoiceItem ciDip;
    private ChoiceItem ciProtocol;
    private ScrollView scrollView;
    private TextView tvLowSetting;
    private TextView tvAccountSetting;
    private TextView tvVideoSetting;
    private ChoiceItem ciScreen;
    private ChoiceItem ciLocal;
    private ChoiceItem ciStandby;
    private Activity context;
    private int frag_pro = 0;
    private int frag_camera = 1;
    private int frag_answer = 0;
    private int frag_lanuch = 0;
    private int frag_update = 0;
    private int frag_local = 0;
    private int frag_decode = 0;
    private int frag_domain = 0;
    private int frag_proxy = 0;
    private int frag_limiter = 0;
    private int frag_auto = 0;
    private int frag_type = 0;
    private int frag_audio = 0;
    private int frag_dip = 0;
    private int frag_noise_gate = 0;
    private int frag_speak_agc = 0;
    private ChoiceItem ciDecode;
    private long[] mHils = new long[4];
    public static final String DISPLAY_OUTPUT_MANAGER = "android.os.DisplayOutputManager";
    private ChoiceItem ciDocument;
    private ChoiceItem ciCamera;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        inflate = View.inflate(context, R.layout.fragment_setting, null);
        initView();
        initData();
        initListener();
        return inflate;
    }

    /**
     * 初始化view
     */
    private void initView() {
        ciName = (ChoiceItem) inflate.findViewById(R.id.ci_name);
        ciAccount = (ChoiceItem) inflate.findViewById(R.id.ci_account);
        ciDomain = (ChoiceItem) inflate.findViewById(R.id.ci_domain);
        ciPassword = (ChoiceItem) inflate.findViewById(R.id.ci_password);
        ciProxy = (ChoiceItem) inflate.findViewById(R.id.ci_proxy);
        ciProtocol = (ChoiceItem) inflate.findViewById(R.id.ci_protocol);
        ciLanguage = (ChoiceItem) inflate.findViewById(R.id.ci_language);
        ciAnswer = (ChoiceItem) inflate.findViewById(R.id.ci_answer);
        ciUpdate = (ChoiceItem) inflate.findViewById(R.id.ci_update);
        ciLaunch = (ChoiceItem) inflate.findViewById(R.id.ci_launch);
        ciDocument = (ChoiceItem) inflate.findViewById(R.id.ci_document);
        ciFrame = (ChoiceItem) inflate.findViewById(R.id.ci_frame);
        ciStream = (ChoiceItem) inflate.findViewById(R.id.ci_stream);
        ciDip = (ChoiceItem) inflate.findViewById(R.id.ci_dip);
        ciScreen = (ChoiceItem) inflate.findViewById(R.id.ci_screen);
        ciLocal = (ChoiceItem) inflate.findViewById(R.id.ci_local);
        ciStandby = (ChoiceItem) inflate.findViewById(R.id.ci_standby);
        ciDecode = (ChoiceItem) inflate.findViewById(R.id.ci_decode);
        ciCamera = (ChoiceItem) inflate.findViewById(R.id.ci_camera);


        tvQuit = (ItemSelectContainer) inflate.findViewById(R.id.tv_quit);
        tvLogin = (ItemSelectContainer) inflate.findViewById(R.id.tv_login);
        scrollView = (ScrollView) inflate.findViewById(R.id.scrollView);
        tvLowSetting = (TextView) inflate.findViewById(R.id.tv_low_setting);
        tvAccountSetting = (TextView) inflate.findViewById(R.id.tv_account_setting);
        tvVideoSetting = (TextView) inflate.findViewById(R.id.tv_video_setting);
    }

    /**
     * 初始化data
     */
    private void initData() {
        if (FaceLogin.instance().getCurrentAuthInfo() != null) {
            ciName.setContentText(FacephonePreferences.instance().getAccountDisplayName(FacephonePreferences.instance().getAccountCount() - 1));
            ciAccount.setContentText(FaceLogin.instance().getCurrentAuthInfo().getUsername());
            ciDomain.setContentText(FaceLogin.instance().getCurrentAuthInfo().getDomain());
            ciProxy.setContentText(FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
            final LinphoneAddress.TransportType transport = FacephonePreferences.instance().getAccountTransport(FacephonePreferences.instance().getAccountCount() - 1);
            if (transport.toInt() == 0) {
                frag_pro = 0;
                ciProtocol.setContentText(R.string.udp);
                SpUtils.putString(context, Constant.PROCOTOL_NAME, "udp");
            } else if (transport.toInt() == 1) {
                frag_pro = 1;
                ciProtocol.setContentText(R.string.tcp);
                SpUtils.putString(context, Constant.PROCOTOL_NAME, "tcp");
            } else if (transport.toInt() == 2) {
                frag_pro = 2;
                ciProtocol.setContentText(R.string.tls);
                SpUtils.putString(context, Constant.PROCOTOL_NAME, "tls");
            }
        }
        String s = context.getResources().getConfiguration().locale.toString();
        if (s.equalsIgnoreCase("zh_CN")) {
            ciLanguage.setContentText((R.string.simple_chinese));
        } else if (s.equalsIgnoreCase("en")) {
            ciLanguage.setContentText((R.string.english));
        } else if (s.equalsIgnoreCase("zh_TW")) {
            ciLanguage.setContentText((R.string.traditional_Chinese));
        }

        //初始化自动接听
        if (FacephonePreferences.instance().isAutoAnswerEnabled()) {
            ciAnswer.setContentText(context.getString(R.string.open));
            frag_answer = 0;
        } else {
            ciAnswer.setContentText(context.getString(R.string.close));
            frag_answer = 1;
        }

        //初始化自动更新
        boolean update = SpUtils.getBoolean(context, Constant.UPDATE_NAME, Constant.UPDATE_DEFAULT);
        if (update) {
            frag_update = 1;
            ciUpdate.setContentText(context.getString(R.string.open));
        } else {
            frag_update = 0;
            ciUpdate.setContentText(context.getString(R.string.close));
        }

        //初始化开机自启
        boolean launch = SpUtils.getBoolean(context, Constant.LAUNCH_NAME, Constant.LAUNCH_DEFAULT);
        if (launch) {
            frag_lanuch = 1;
            ciLaunch.setContentText(context.getString(R.string.open));
        } else {
            frag_lanuch = 0;
            ciLaunch.setContentText(context.getString(R.string.close));
        }

        //初始化帧率
        ciFrame.setContentText(FacephonePreferences.instance().getPreferredVideoFps() + "");

        //初始化码流
        ciStream.setContentText(FacephonePreferences.instance().getVideoBitrate() + "");

        //初始化分辨率
        ciDip.setContentText(FacephonePreferences.instance().getPreferredVideoSize());

        //初始化待机
        try {
            int anInt = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
            ciStandby.setContentText(anInt / (1000 * 60) + "分钟");
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        //初始化本地背景
        int frag = SpUtils.getInt(context, Constant.BACKGROUND_NAME, Constant.BACKGROUND_DEFAULT);
        if (frag == 1) {
            frag_local = 1;
            ciLocal.setContentText(context.getString(R.string.local_camera));
        } else if (frag == 2) {
            frag_local = 0;
            ciLocal.setContentText(context.getString(R.string.local_bitmap));
        }
        //保存代理和域名
        SpUtils.putString(context, Constant.PROXY_NAME, FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
        SpUtils.putString(context, Constant.DOMAIN_NAME, FaceLogin.instance().getCurrentAuthInfo().getDomain());

        //初始化视频解码
        int decode = FacephonePreferences.instance().getVideoDecode();
        if (decode == 0) {
            ciDecode.setContentText(R.string.soff);
        } else if (decode == 1) {
            ciDecode.setContentText(R.string.hardward);
        }
        //初始化分辨率
        ciDip.setContentText(FacephonePreferences.instance().getPreferredVideoSize());


        //初始化摄像头
        int encode = FacephonePreferences.instance().getVideoEncode();
        if (encode == 5) {
            frag_camera = 5;
            ciCamera.setContentText(context.getString(R.string.major_camera));
        } else {
            frag_camera = 1;
            ciCamera.setContentText(context.getString(R.string.general_camera));
        }
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        ciName.setOnClickListener(this);
        ciAccount.setOnClickListener(this);
//        ciDomain.setOnClickListener(this);
        ciPassword.setOnClickListener(this);
//        ciProxy.setOnClickListener(this);
        ciProtocol.setOnClickListener(this);
        ciLanguage.setOnClickListener(this);
        ciCamera.setOnClickListener(this);
        ciAnswer.setOnClickListener(this);
        ciUpdate.setOnClickListener(this);
        ciLaunch.setOnClickListener(this);
        ciFrame.setOnClickListener(this);
        ciStream.setOnClickListener(this);
        ciDocument.setOnClickListener(this);
        ciScreen.setOnClickListener(this);
        ciLocal.setOnClickListener(this);
        ciStandby.setOnClickListener(this);
        ciDecode.setOnClickListener(this);
        tvLogin.setOnClickListener(this);
        tvQuit.setOnClickListener(this);
        ciDip.setOnClickListener(this);
        ciCamera.setOnFocusChangeListener(this);
        tvAccountSetting.setOnFocusChangeListener(this);
        tvVideoSetting.setOnFocusChangeListener(this);
        tvLowSetting.setOnFocusChangeListener(this);
        ciName.setOnFocusChangeListener(this);
        ciAccount.setOnFocusChangeListener(this);
        ciDomain.setOnFocusChangeListener(this);
        ciPassword.setOnFocusChangeListener(this);
        ciProxy.setOnFocusChangeListener(this);
        ciProtocol.setOnFocusChangeListener(this);
        ciLanguage.setOnFocusChangeListener(this);
        ciDocument.setOnFocusChangeListener(this);
        ciAnswer.setOnFocusChangeListener(this);
        ciUpdate.setOnFocusChangeListener(this);
        ciLaunch.setOnFocusChangeListener(this);
        ciFrame.setOnFocusChangeListener(this);
        ciStream.setOnFocusChangeListener(this);

        ciScreen.setOnFocusChangeListener(this);
        ciLocal.setOnFocusChangeListener(this);
        ciStandby.setOnFocusChangeListener(this);
        tvLogin.setOnFocusChangeListener(this);
        tvQuit.setOnFocusChangeListener(this);
        ciDecode.setOnFocusChangeListener(this);
        ciDip.setOnFocusChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ci_camera:
                final CustomDialog dialogCamera = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogCamera.show();
                }
                View inflateCamera = View.inflate(context, R.layout.dialog_camera, null);
                RadioGroup rgCamera = (RadioGroup) inflateCamera.findViewById(R.id.rg_camera);
                final RadioButton rbCamera1 = (RadioButton) inflateCamera.findViewById(R.id.rb_camera_1);
                final RadioButton rbCamera2 = (RadioButton) inflateCamera.findViewById(R.id.rb_camera_2);
                dialogCamera.setTitleText(R.string.change_camera);
                dialogCamera.setDeclareText(R.string.change_camera_hint);
                dialogCamera.getContentView().addView(inflateCamera);
                //1代表普通摄像头  5代表专业摄像头
                int encode = FacephonePreferences.instance().getVideoEncode();
                if (encode == 5) {
                    rbCamera2.setChecked(true);
                } else {
                    rbCamera1.setChecked(true);
                }

                final int fragc = frag_camera;
                rgCamera.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        if (checkedId == R.id.rb_camera_1) {
                            frag_camera = 1;
                        }
                        if (checkedId == R.id.rb_camera_2) {
                            frag_camera = 5;
                        }
                    }
                });
                dialogCamera.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_camera = fragc;
                        dialogCamera.dismiss();

                    }
                });
                dialogCamera.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_camera == 1) {
                            FacephonePreferences.instance().setVideoEncode(1);
                            ciCamera.setContentText(context.getString(R.string.general_camera));
                        } else if (frag_camera == 5) {
                            FacephonePreferences.instance().setVideoEncode(5);
                            ciCamera.setContentText(context.getString(R.string.major_camera));
                        }
                        dialogCamera.dismiss();
                    }
                });

                break;
            case R.id.ci_name:

                final CustomDialog dialogName = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogName.show();
                }
                View inflateName = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etNameContent = (EditText) inflateName.findViewById(R.id.et_conetent);

                dialogName.getContentView().addView(inflateName);
                dialogName.setTitleText(R.string.alter_name);
                dialogName.setDeclareText(R.string.name_explain);
                dialogName.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogName.dismiss();
                    }
                });

                etNameContent.setText(FacephonePreferences.instance().getAccountDisplayName(FacephonePreferences.instance().getAccountCount() - 1));
                etNameContent.setSelection(etNameContent.getText().length());
                dialogName.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        dialogName.dismiss();
                        String name = etNameContent.getText().toString();
                        FaceLogin.instance().alterDisplayName(name);
                        // SpUtils.putString(context, Constant.DISPLAY_NAME, name);
                        ciName.setContentText(FacephonePreferences.instance().getAccountDisplayName(FacephonePreferences.instance().getAccountCount() - 1));

                    }
                });

                etNameContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etNameContent, InputType.TYPE_CLASS_TEXT);
                    }
                });
                break;
            case R.id.ci_account:
                final CustomDialog accountDialog = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    accountDialog.show();
                }
                View inflateAccount = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etAccountContent = (EditText) inflateAccount.findViewById(R.id.et_conetent);

                accountDialog.getContentView().addView(inflateAccount);
                accountDialog.setTitleText(R.string.alter_account);
                accountDialog.setDeclareText(R.string.account_explain);
                accountDialog.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        accountDialog.dismiss();
                    }
                });
                etAccountContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                etAccountContent.setText((FaceLogin.instance().getCurrentAuthInfo().getUsername()));
                etAccountContent.setSelection(etAccountContent.getText().length());
                accountDialog.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String account = etAccountContent.getText().toString();
                        if (TextUtils.isEmpty(account)) {
                            Toast.makeText(context, R.string.content_null_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        accountDialog.dismiss();
                        FaceLogin.instance().alterAccountName(account);
                        // FaceLogin.instance().alterDisplayName(SpUtils.getString(context, Constant.DISPLAY_NAME, ""));
                        ciAccount.setContentText((FaceLogin.instance().getCurrentAuthInfo().getUsername()));
                    }
                });
                etAccountContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etAccountContent, InputType.TYPE_CLASS_NUMBER);
                    }
                });
                break;
            case R.id.ci_password:
                final CustomDialog dialogPassword = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogPassword.show();
                }
                View inflatePassword = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etContentPassword = (EditText) inflatePassword.findViewById(R.id.et_conetent);

                dialogPassword.getContentView().addView(inflatePassword);
                dialogPassword.setTitleText(R.string.alter_password);
                dialogPassword.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogPassword.dismiss();
                    }
                });
                etContentPassword.setInputType(InputType.TYPE_CLASS_NUMBER);
                etContentPassword.setText((FaceLogin.instance().getCurrentAuthInfo().getPassword()));
                etContentPassword.setSelection(etContentPassword.getText().length());
                dialogPassword.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String password = etContentPassword.getText().toString();
                        if (TextUtils.isEmpty(password)) {
                            Toast.makeText(context, R.string.content_null_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        dialogPassword.dismiss();
                        // SpUtils.putString(context, Constant.PASSWORD_NAME, password);
//                        FacePhone.instance().getmLc().refreshRegisters();
                        FaceLogin.instance().alterAcountPwd(password);
                    }
                });

                etContentPassword.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etContentPassword, InputType.TYPE_CLASS_NUMBER);

                    }
                });
                break;
            case R.id.ci_domain:
                final CustomDialog dialogDomain = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogDomain.show();
                }
                View inflateDomain = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etContentDomain = (EditText) inflateDomain.findViewById(R.id.et_conetent);

                dialogDomain.getContentView().addView(inflateDomain);
                dialogDomain.setTitleText(R.string.alter_domain);
                dialogDomain.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogDomain.dismiss();
                    }
                });
                etContentDomain.setText((FaceLogin.instance().getCurrentAuthInfo().getDomain()));
                etContentDomain.setSelection(etContentDomain.getText().length());
                dialogDomain.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String domain = etContentDomain.getText().toString();
                        if (!isIP(domain, false)) {
                            Toast.makeText(context, R.string.ip_format_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        FaceLogin.instance().alterDomain(domain);
                        ciDomain.setContentText((FaceLogin.instance().getCurrentAuthInfo().getDomain()));
                        SpUtils.putString(context, Constant.DOMAIN_NAME, FaceLogin.instance().getCurrentAuthInfo().getDomain());
                        dialogDomain.dismiss();
                    }
                });
                etContentDomain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etContentDomain, InputType.TYPE_CLASS_TEXT);

                    }
                });

//                View inflateDomain = View.inflate(context, R.layout.dialog_camera, null);
//                RadioGroup rgDomain = (RadioGroup) inflateDomain.findViewById(R.id.rg_camera);
//                final RadioButton rbIP1 = (RadioButton) inflateDomain.findViewById(R.id.rb_camera_1);
//                final RadioButton rbIP2 = (RadioButton) inflateDomain.findViewById(R.id.rb_camera_2);
//                TextView tvDomainSure = (TextView) inflateDomain.findViewById(R.id.submit);
//                TextView tvDomainCancel = (TextView) inflateDomain.findViewById(R.id.cancel);
//                TextView tvDomainTitle = (TextView) inflateDomain.findViewById(R.id.title);
//                tvDomainTitle.setText("选择IP");
//                rbIP1.setText(Constant.IP);
//                rbIP2.setText("10.21.15.7:5060");
//                //1代表普通摄像头  5代表专业摄像头
//                String domain = FaceLogin.instance().getCurrentAuthInfo().getDomain();
//                if (domain.equals("10.21.16.4")) {
//                    rbIP1.setChecked(true);
//                } else if (domain.equals("10.21.15.7")) {
//                    rbIP2.setChecked(true);
//                }
//                final CustomDialog dialogDomain = new CustomDialog(context);
//                if (!getActivity().isFinishing()) {
//                    dialogDomain.show();
//                }
//                dialogDomain.initConfig(inflateDomain);
//
//                final int fragdo = frag_domain;
//                rgDomain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//                        if (checkedId == R.id.rb_camera_1) {
//                            frag_camera = 0;
//                        }
//                        if (checkedId == R.id.rb_camera_2) {
//                            frag_camera = 1;
//                        }
//                    }
//                });
//                tvDomainSure.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (frag_camera == 0) {
//                            FaceLogin.instance().alterDomain(Constant.IP);
//                        } else if (frag_camera == 1) {
//                            FaceLogin.instance().alterDomain("10.21.15.7:5060");
//                        }
//                        ciDomain.setContentText((FaceLogin.instance().getCurrentAuthInfo().getDomain()));
//                        dialogDomain.dismiss();
//                    }
//                });
//                tvDomainCancel.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        frag_camera = fragdo;
//                        dialogDomain.dismiss();
//                    }
//                });
                break;
            case R.id.ci_proxy:
                final CustomDialog dialogProxy = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogProxy.show();
                }
                View inflateProxy = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etContentProxy = (EditText) inflateProxy.findViewById(R.id.et_conetent);

                dialogProxy.getContentView().addView(inflateProxy);
                dialogProxy.setTitleText(R.string.alter_proxy);
                dialogProxy.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogProxy.dismiss();
                    }
                });

                String proxy = FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1);
                etContentProxy.setText(proxy.substring(5, proxy.length() - 15));
                etContentProxy.setSelection(etContentProxy.getText().length());
                dialogProxy.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        dialogProxy.dismiss();
                        String proxy = etContentProxy.getText().toString();
                        if (!isIP(proxy, true)) {
                            Toast.makeText(context, R.string.proxy_format_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        final LinphoneAddress.TransportType transport = FacephonePreferences.instance().getAccountTransport(FacephonePreferences.instance().getAccountCount() - 1);
                        String tran = "";
                        if (transport.toInt() == 0) {
                            tran = "udp";
                        } else if (transport.toInt() == 1) {
                            tran = "tcp";
                        } else if (transport.toInt() == 2) {
                            tran = "tls";
                        }
                        FaceLogin.instance().alterProxy("<sip:" + proxy + ";transport=" + tran + ">");
                        ciProxy.setContentText(FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
                        SpUtils.putString(context, Constant.PROXY_NAME, FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
                    }
                });
                etContentProxy.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etContentProxy, InputType.TYPE_CLASS_TEXT);
                    }
                });

//                View inflateProxy = View.inflate(context, R.layout.dialog_camera, null);
//                RadioGroup rgProxy = (RadioGroup) inflateProxy.findViewById(R.id.rg_camera);
//                final RadioButton rbProxy1 = (RadioButton) inflateProxy.findViewById(R.id.rb_camera_1);
//                final RadioButton rbProxy2 = (RadioButton) inflateProxy.findViewById(R.id.rb_camera_2);
//                TextView tvProxySure = (TextView) inflateProxy.findViewById(R.id.submit);
//                TextView tvProxyCancel = (TextView) inflateProxy.findViewById(R.id.cancel);
//                TextView tvProxyTitle = (TextView) inflateProxy.findViewById(R.id.title);
//                tvProxyTitle.setText("选择代理");
//                rbProxy1.setText(Constant.PROXY);
//                rbProxy2.setText("sip:10.21.15.7:5060");
//                //1代表普通摄像头  5代表专业摄像头
//                String proxy = FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1);
//                proxy = proxy.substring(5, proxy.length() - 15);
//                if (proxy.equals("10.21.16.4:5080")) {
//                    rbProxy1.setChecked(true);
//                } else if (proxy.equals("10.21.15.7:5060")) {
//                    rbProxy2.setChecked(true);
//                }
//                final CustomDialog dialogProxy = new CustomDialog(context);
//                if (!getActivity().isFinishing()) {
//                    dialogProxy.show();
//                }
//                dialogProxy.initConfig(inflateProxy);
//
//                final int frag_pr = frag_proxy;
//                rgProxy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//                    @Override
//                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//                        if (checkedId == R.id.rb_camera_1) {
//                            frag_proxy = 0;
//                        }
//                        if (checkedId == R.id.rb_camera_2) {
//                            frag_proxy = 1;
//                        }
//                    }
//                });
//                tvProxySure.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        final LinphoneAddress.TransportType transport = FacephonePreferences.instance().getAccountTransport(FacephonePreferences.instance().getAccountCount() - 1);
//                        String tran = "";
//                        if (transport.toInt() == 0) {
//                            tran = "udp";
//                        } else if (transport.toInt() == 1) {
//                            tran = "tcp";
//                        } else if (transport.toInt() == 2) {
//                            tran = "tls";
//                        }
//                        if (frag_proxy == 0) {
//                            FaceLogin.instance().alterProxy("<sip:" + "10.21.16.4:5080" + ";transport=" + tran + ">");
//                        } else if (frag_proxy == 1) {
//                            FaceLogin.instance().alterProxy("<sip:" + "10.21.15.7:5060" + ";transport=" + tran + ">");
//                        }
//                        ciProxy.setContentText(FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
//                        dialogProxy.dismiss();
//                    }
//                });
//                tvProxyCancel.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        frag_proxy = frag_pr;
//                        dialogProxy.dismiss();
//                    }
//                });
                break;
            case R.id.ci_protocol:
                final CustomDialog dialogProtocol = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogProtocol.show();
                }
                View inflateProtocol = View.inflate(context, R.layout.dialog_protocol, null);
                RadioGroup rgProtocol = (RadioGroup) inflateProtocol.findViewById(R.id.rg_protocol);
                final RadioButton rbTcp = (RadioButton) inflateProtocol.findViewById(R.id.rb_tcp);
                final RadioButton rbUdp = (RadioButton) inflateProtocol.findViewById(R.id.rb_udp);
                final RadioButton rbTls = (RadioButton) inflateProtocol.findViewById(R.id.rb_tls);

                dialogProtocol.setTitleText(R.string.alter_pro);
                dialogProtocol.setDeclareText(R.string.pro_hint);
                dialogProtocol.getContentView().addView(inflateProtocol);
                final LinphoneAddress.TransportType transport = FacephonePreferences.instance().getAccountTransport(FacephonePreferences.instance().getAccountCount() - 1);
                if (transport.toInt() == 0) {
                    rbUdp.setChecked(true);
                } else if (transport.toInt() == 1) {
                    rbTcp.setChecked(true);
                } else if (transport.toInt() == 2) {
                    rbTls.setChecked(true);
                }
                rgProtocol.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_tcp:
                                frag_pro = 0;
                                break;
                            case R.id.rb_udp:
                                frag_pro = 1;
                                break;
                            case R.id.rb_tls:
                                frag_pro = 2;
                                break;
                        }
                    }
                });
                dialogProtocol.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogProtocol.dismiss();
                    }
                });
                dialogProtocol.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_pro == 0) {
                            FacephonePreferences.instance().setAccountTransport(FacephonePreferences.instance().getAccountCount() - 1, "tcp");
                        } else if (frag_pro == 1) {
                            FacephonePreferences.instance().setAccountTransport(FacephonePreferences.instance().getAccountCount() - 1, "udp");
                        } else if (frag_pro == 2) {
                            FacephonePreferences.instance().setAccountTransport(FacephonePreferences.instance().getAccountCount() - 1, "tls");
                        }
                        final LinphoneAddress.TransportType transport = FacephonePreferences.instance().getAccountTransport(FacephonePreferences.instance().getAccountCount() - 1);
                        if (transport.toInt() == 0) {
                            ciProtocol.setContentText(R.string.udp);
                            SpUtils.putString(context, Constant.PROCOTOL_NAME, "udp");
                        } else if (transport.toInt() == 1) {
                            ciProtocol.setContentText(R.string.tcp);
                            SpUtils.putString(context, Constant.PROCOTOL_NAME, "tcp");
                        } else if (transport.toInt() == 2) {
                            ciProtocol.setContentText(R.string.tls);
                            SpUtils.putString(context, Constant.PROCOTOL_NAME, "tls");
                        }
                        dialogProtocol.dismiss();
                    }
                });

                break;
            case R.id.ci_language:
                final CustomDialog dialogLanguage = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogLanguage.show();
                }
                View inflateLanguage = View.inflate(context, R.layout.dialog_protocol, null);
                RadioGroup rgLanguage = (RadioGroup) inflateLanguage.findViewById(R.id.rg_protocol);
                final RadioButton rbz = (RadioButton) inflateLanguage.findViewById(R.id.rb_tcp);
                final RadioButton rbf = (RadioButton) inflateLanguage.findViewById(R.id.rb_udp);
                final RadioButton rby = (RadioButton) inflateLanguage.findViewById(R.id.rb_tls);
                dialogLanguage.setTitleText(R.string.switch_the_language);
                dialogLanguage.getContentView().addView(inflateLanguage);
                rbf.setText(R.string.traditional_Chinese);
                rby.setText(R.string.english);
                rbz.setText(R.string.simple_chinese);
                if (context.getResources().getConfiguration().locale.toString().equals("zh_TW")) {
                    rbf.setChecked(true);
                } else if (context.getResources().getConfiguration().locale.toString().equals("en")) {
                    rby.setChecked(true);
                } else if (context.getResources().getConfiguration().locale.toString().equals("zh_CN")) {
                    rbz.setChecked(true);
                }

                rgLanguage.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        switch (checkedId) {
                            case R.id.rb_tcp:
                                SpUtils.putString(context, Constant.LOCAL_NAME, "zh_CN");
                                break;
                            case R.id.rb_udp:
                                SpUtils.putString(context, Constant.LOCAL_NAME, "zh_TW");
                                break;
                            case R.id.rb_tls:
                                SpUtils.putString(context, Constant.LOCAL_NAME, "en");

                                break;
                        }
                    }
                });
                dialogLanguage.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        SpUtils.putString(context,
                                Constant.LOCAL_NAME,
                                context.getResources().getConfiguration().locale.toString());
                        dialogLanguage.dismiss();
                    }
                });
                dialogLanguage.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        dialogLanguage.dismiss();
                        SpUtils.putBoolean(context, "isback", true);
                        String local = SpUtils.getString(context, Constant.LOCAL_NAME, "zh_CN");
                        if (local.equalsIgnoreCase("zh_CN")) {
                            switchLanguage(Locale.SIMPLIFIED_CHINESE);
                        } else if (local.equalsIgnoreCase("en")) {
                            switchLanguage(Locale.ENGLISH);
                        } else if (local.equalsIgnoreCase("zh_TW")) {
                            switchLanguage(Locale.TRADITIONAL_CHINESE);
                        }
                        context.recreate();
                    }
                });

                break;

            case R.id.ci_answer:
                final CustomDialog dialogAnswer = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogAnswer.show();
                }
                View inflateAnswer = View.inflate(context, R.layout.dialog_answer, null);
                RadioGroup rgAnswer = (RadioGroup) inflateAnswer.findViewById(R.id.rg_answer);
                final RadioButton rbOpen = (RadioButton) inflateAnswer.findViewById(R.id.rb_open);
                final RadioButton rbClose = (RadioButton) inflateAnswer.findViewById(R.id.rb_close);
                dialogAnswer.setTitleText(R.string.answer);
                dialogAnswer.setDeclareText(R.string.answer_hint);
                dialogAnswer.getContentView().addView(inflateAnswer);
                if (FacephonePreferences.instance().isAutoAnswerEnabled()) {
                    rbOpen.setChecked(true);
                } else {
                    rbClose.setChecked(true);
                }
                final int fraga = frag_answer;

                rgAnswer.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                        if (checkedId == R.id.rb_open) {
                            frag_answer = 1;
                        }
                        if (checkedId == R.id.rb_close) {
                            frag_answer = 0;
                        }


                    }
                });
                dialogAnswer.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_answer == 0) {
                            FacephonePreferences.instance().enableAutoAnswer(false);
                        } else if (frag_answer == 1) {
                            FacephonePreferences.instance().enableAutoAnswer(true);
                        }
                        if (FacephonePreferences.instance().isAutoAnswerEnabled()) {
                            ciAnswer.setContentText(context.getString(R.string.open));
                        } else {
                            ciAnswer.setContentText(context.getString(R.string.close));
                        }
                        dialogAnswer.dismiss();
                    }
                });
                dialogAnswer.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_answer = fraga;
                        dialogAnswer.dismiss();
                    }
                });

                break;

            case R.id.ci_update:
                final CustomDialog dialogUpdate = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogUpdate.show();
                }
                View inflateUpdate = View.inflate(context, R.layout.dialog_answer, null);
                RadioGroup rgUpdate = (RadioGroup) inflateUpdate.findViewById(R.id.rg_answer);
                final RadioButton rbUpdateOpen = (RadioButton) inflateUpdate.findViewById(R.id.rb_open);
                final RadioButton rbUpdateClose = (RadioButton) inflateUpdate.findViewById(R.id.rb_close);
                dialogUpdate.setTitleText(R.string.update);
                dialogUpdate.setDeclareText(R.string.update_hint);
                dialogUpdate.getContentView().addView(inflateUpdate);
                //初始化自动更新
                boolean b = SpUtils.getBoolean(context, Constant.UPDATE_NAME, Constant.UPDATE_DEFAULT);
                if (b) {
                    rbUpdateOpen.setChecked(true);
                } else {
                    rbUpdateClose.setChecked(true);
                }

                final int fargu = frag_update;

                rgUpdate.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                        if (checkedId == R.id.rb_open) {
                            frag_update = 1;

                        }
                        if (checkedId == R.id.rb_close) {
                            frag_update = 0;

                        }

                    }
                });
                dialogUpdate.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_update = fargu;
                        dialogUpdate.dismiss();
                    }
                });
                dialogUpdate.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_update == 0) {
                            SpUtils.putBoolean(context, Constant.UPDATE_NAME, false);
                            ciUpdate.setContentText(context.getString(R.string.close));
                        }
                        if (frag_update == 1) {
                            SpUtils.putBoolean(context, Constant.UPDATE_NAME, Constant.UPDATE_DEFAULT);
                            ciUpdate.setContentText(context.getString(R.string.open));
                        }
                        dialogUpdate.dismiss();
                    }
                });

                break;

            case R.id.ci_launch:
                final CustomDialog dialogLaunch = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogLaunch.show();
                }
                View inflateLaunch = View.inflate(context, R.layout.dialog_answer, null);
                RadioGroup rgLaunch = (RadioGroup) inflateLaunch.findViewById(R.id.rg_answer);
                final RadioButton rbLaunchOpen = (RadioButton) inflateLaunch.findViewById(R.id.rb_open);
                final RadioButton rbLaunchClose = (RadioButton) inflateLaunch.findViewById(R.id.rb_close);
                dialogLaunch.setTitleText(R.string.launch);
                dialogLaunch.setDeclareText(R.string.launch_hint);
                dialogLaunch.getContentView().addView(inflateLaunch);
                //初始化自动更新
                boolean b1 = SpUtils.getBoolean(context, Constant.LAUNCH_NAME, Constant.LAUNCH_DEFAULT);
                if (b1) {
                    rbLaunchOpen.setChecked(true);
                } else {
                    rbLaunchClose.setChecked(true);
                }

                final int fragl = frag_lanuch;

                rgLaunch.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                        if (checkedId == R.id.rb_open) {
                            frag_lanuch = 1;
                        }
                        if (checkedId == R.id.rb_close) {
                            frag_lanuch = 0;
                        }

                    }
                });
                dialogLaunch.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_lanuch = fragl;
                        dialogLaunch.dismiss();
                    }
                });
                dialogLaunch.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_lanuch == 1) {
                            SpUtils.putBoolean(context, Constant.LAUNCH_NAME, true);
                            ciLaunch.setContentText(context.getString(R.string.open));
                        }
                        if (frag_lanuch == 0) {
                            SpUtils.putBoolean(context, Constant.LAUNCH_NAME, false);
                            ciLaunch.setContentText(context.getString(R.string.close));
                        }
                        dialogLaunch.dismiss();
                    }
                });

                break;

            case R.id.ci_frame:
                final CustomDialog dialogFrame = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogFrame.show();
                }
                View inflateFrame = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etFrameContent = (EditText) inflateFrame.findViewById(R.id.et_conetent);

                dialogFrame.setTitleText(R.string.frame);
                dialogFrame.setDeclareText(R.string.frame_hint);
                dialogFrame.getContentView().addView(inflateFrame);

                etFrameContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                etFrameContent.setText(FacephonePreferences.instance().getPreferredVideoFps() + "");
                etFrameContent.setSelection(etFrameContent.getText().length());
                dialogFrame.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogFrame.dismiss();
                    }
                });
                dialogFrame.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String frame = etFrameContent.getText().toString();
                        if (TextUtils.isEmpty(frame)) {
                            Toast.makeText(context, R.string.content_null_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (frame.length() > 2) {
                            Toast.makeText(context, R.string.length_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //设置帧率
                        FacephonePreferences.instance().setPreferredVideoFps(Integer.parseInt(frame));
                        ciFrame.setContentText(FacephonePreferences.instance().getPreferredVideoFps() + "");
                        Toast.makeText(context, R.string.operation_success, Toast.LENGTH_SHORT).show();
                        dialogFrame.dismiss();
                    }
                });

                etFrameContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        popuSoft(etFrameContent, InputType.TYPE_CLASS_NUMBER);
                    }
                });
                break;

            case R.id.ci_stream:
                final CustomDialog dialogStream = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogStream.show();
                }
                View inflateStream = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etStreamContent = (EditText) inflateStream.findViewById(R.id.et_conetent);

                dialogStream.setTitleText(R.string.stream);
                dialogStream.setDeclareText(R.string.stream_hint);
                dialogStream.getContentView().addView(inflateStream);
                dialogStream.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogStream.dismiss();
                    }
                });
                etStreamContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                etStreamContent.setText(FacephonePreferences.instance().getVideoBitrate() + "");
                etStreamContent.setSelection(etStreamContent.getText().length());
                dialogStream.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String stream = etStreamContent.getText().toString();
                        if (TextUtils.isEmpty(stream)) {
                            Toast.makeText(context, R.string.content_null_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (stream.length() >= 5) {
                            Toast.makeText(context, R.string.length_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }

                        dialogStream.dismiss();
                        //设置码流
                        FacephonePreferences.instance().setVideoBitrate(Integer.parseInt(stream));
                        ciStream.setContentText(FacephonePreferences.instance().getVideoBitrate() + "");
                    }
                });

                etStreamContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etStreamContent, InputType.TYPE_CLASS_NUMBER);
                    }
                });
                break;

            case R.id.tv_quit:
                QuitEvent event = new QuitEvent();
                EventBus.getDefault().post(event);
                break;

            case R.id.tv_login:
                FacePhone.instance().getmLc().setNetworkReachable(true);
                FacePhone.instance().getmLc().refreshRegisters();
                System.arraycopy(mHils, 1, mHils, 0, mHils.length - 1);
                mHils[mHils.length - 1] = SystemClock.uptimeMillis();
                if (mHils[0] >= SystemClock.uptimeMillis() - 500) {
                    Intent intent = new Intent(context, DeveloperActivity.class);
                    startActivity(intent);
                }
                break;

            case R.id.ci_local:
                final CustomDialog dialogLocal = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogLocal.show();
                }
                View inflateLocal = View.inflate(context, R.layout.dialog_camera, null);
                RadioGroup rgLocal = (RadioGroup) inflateLocal.findViewById(R.id.rg_camera);
                final RadioButton rbCamera = (RadioButton) inflateLocal.findViewById(R.id.rb_camera_1);
                final RadioButton rbBitmap = (RadioButton) inflateLocal.findViewById(R.id.rb_camera_2);

                dialogLocal.setTitleText(R.string.change_bg);
                dialogLocal.setDeclareText(R.string.change_bg_hint);
                dialogLocal.getContentView().addView(inflateLocal);
                rbCamera.setText(context.getString(R.string.local_camera));
                rbBitmap.setText(context.getString(R.string.local_bitmap));
                //1代表本地画面  2代表本地图片
                int frag = SpUtils.getInt(context, Constant.BACKGROUND_NAME, Constant.BACKGROUND_DEFAULT);
                if (frag == Constant.BACKGROUND_DEFAULT) {
                    rbCamera.setChecked(true);
                } else {
                    rbBitmap.setChecked(true);
                }

                final int fragb = frag_local;
                rgLocal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        if (checkedId == R.id.rb_camera_1) {
                            frag_local = 1;
                        }
                        if (checkedId == R.id.rb_camera_2) {
                            frag_local = 2;
                        }
                    }
                });
                dialogLocal.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_local == 2) {
                            SpUtils.putInt(context, Constant.BACKGROUND_NAME, Constant.BACKGROUND_VALUE);
                            ciLocal.setContentText(context.getString(R.string.local_bitmap));

                        }
                        if (frag_local == 1) {
                            SpUtils.putInt(context, Constant.BACKGROUND_NAME, Constant.BACKGROUND_DEFAULT);
                            ciLocal.setContentText(context.getString(R.string.local_camera));

                        }
                        dialogLocal.dismiss();
                        RestartAPPTool.restartAPP(context);
                    }
                });
                dialogLocal.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_local = fragb;
                        dialogLocal.dismiss();
                    }
                });

                break;

            case R.id.ci_screen:
                final CustomDialog dialogScreen = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogScreen.show();
                }
                View inflateScreen = View.inflate(context, R.layout.dialog_screen, null);
                ImageView ivScreenReduce = (ImageView) inflateScreen.findViewById(R.id.iv_reduce_landscape);
                ImageView ivScreenAdd = (ImageView) inflateScreen.findViewById(R.id.iv_add_landscape);
                ImageView ivScreenUp = (ImageView) inflateScreen.findViewById(R.id.iv_screen_up);
                ImageView ivScreenDown = (ImageView) inflateScreen.findViewById(R.id.iv_screen_down);

                dialogScreen.setTitleText(R.string.screen_zoom);
                dialogScreen.setDeclareText(R.string.screen_zoom_hint);
                dialogScreen.getContentView().addView(inflateScreen);

                ivScreenReduce.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Rect rect = getOverScan();
                        if (rect.left > 80) {
                            setOverScan(0, rect.left - 1, 0);
                        }
                    }
                });
                ivScreenAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Rect rect = getOverScan();
                        if (rect.left < 100) {
                            setOverScan(0, rect.left + 1, 0);
                        }

                    }
                });
                ivScreenUp.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Rect rect = getOverScan();
                        if (rect.top < 100) {
                            setOverScan(1, rect.top + 1, 0);
                        }
                    }
                });
                ivScreenDown.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Rect rect = getOverScan();
                        if (rect.top > 80) {
                            setOverScan(1, rect.top - 1, 0);
                        }
                    }
                });
                dialogScreen.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogScreen.dismiss();

                    }
                });

                dialogScreen.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        dialogScreen.dismiss();
                    }
                });
                break;
            case R.id.ci_standby:
                final CustomDialog dialogStandby = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogStandby.show();
                }
                View inflateStandby = View.inflate(context, R.layout.dialog_acount, null);
                final EditText etStandbyContent = (EditText) inflateStandby.findViewById(R.id.et_conetent);

                dialogStandby.setTitleText(R.string.standby);
                dialogStandby.setDeclareText(R.string.standby_hint);
                dialogStandby.getContentView().addView(inflateStandby);
                dialogStandby.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogStandby.dismiss();

                    }
                });
                try {
                    int anInt = Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
                    etStandbyContent.setText(anInt / (60 * 1000) + "");
                } catch (Settings.SettingNotFoundException e) {
                    e.printStackTrace();
                }
                etStandbyContent.setSelection(etStandbyContent.getText().length());
                dialogStandby.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        String time = etStandbyContent.getText().toString();
                        if (TextUtils.isEmpty(time)) {
                            Toast.makeText(context, R.string.content_null_hint, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (Integer.parseInt(time) <= 0) {
                            Toast.makeText(context, R.string.number_judge, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (Integer.parseInt(time) > 5 * 60) {
                            Toast.makeText(context, R.string.timeout, Toast.LENGTH_SHORT).show();
                            return;
                        }
                        dialogStandby.dismiss();
                        try {
                            Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, Integer.valueOf(time) * 60 * 1000);
                            SpUtils.putInt(context, Constant.LOCK_NAME, Integer.valueOf(time));
                            ciStandby.setContentText(time + "");
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(context, R.string.operation_success, Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                etStandbyContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popuSoft(etStandbyContent, InputType.TYPE_CLASS_NUMBER);
                    }
                });
                break;
            case R.id.ci_decode:
                final CustomDialog dialogDecode = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogDecode.show();
                }
                View inflateDecode = View.inflate(context, R.layout.dialog_camera, null);
                RadioGroup rgDecode = (RadioGroup) inflateDecode.findViewById(R.id.rg_camera);
                final RadioButton rbrj = (RadioButton) inflateDecode.findViewById(R.id.rb_camera_1);
                final RadioButton rbyj = (RadioButton) inflateDecode.findViewById(R.id.rb_camera_2);
                dialogDecode.setTitleText(R.string.decode);
                dialogDecode.setDeclareText(R.string.decode_hint);
                dialogDecode.getContentView().addView(inflateDecode);
                rbrj.setText(R.string.soff);
                rbyj.setText(R.string.hardward);

                //初始化解码
                int decode = FacephonePreferences.instance().getVideoDecode();
                if (decode == 0) {
                    rbrj.setChecked(true);
                } else if (decode == 1) {
                    rbyj.setChecked(true);
                }

                final int fragd = frag_decode;
                rgDecode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        if (checkedId == R.id.rb_camera_1) {
                            frag_decode = 0;
                        }
                        if (checkedId == R.id.rb_camera_2) {
                            frag_decode = 1;
                        }
                    }
                });
                dialogDecode.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_decode = fragd;
                        dialogDecode.dismiss();
                    }
                });

                dialogDecode.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_decode == 0) {
                            FacephonePreferences.instance().setVideoDecode(0);
                            ciDecode.setContentText(R.string.soff);

                        }
                        if (frag_decode == 1) {
                            FacephonePreferences.instance().setVideoDecode(1);
                            ciDecode.setContentText(R.string.hardward);
                        }
                        dialogDecode.dismiss();
                    }
                });
                break;
            case R.id.ci_dip:
                final CustomDialog dialogDip = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogDip.show();
                }
                View inflateDip = View.inflate(context, R.layout.dialog_camera, null);
                RadioGroup rgDip = (RadioGroup) inflateDip.findViewById(R.id.rg_camera);
                final RadioButton rbDip720 = (RadioButton) inflateDip.findViewById(R.id.rb_camera_1);
                final RadioButton rbDip1280 = (RadioButton) inflateDip.findViewById(R.id.rb_camera_2);

                dialogDip.setTitleText(R.string.dip);
                dialogDip.setDeclareText(R.string.dip_hint);
                dialogDip.getContentView().addView(inflateDip);
                rbDip720.setText(context.getString(R.string.dip_720));
                rbDip1280.setText(context.getString(R.string.dip_1280));
                String size = FacephonePreferences.instance().getPreferredVideoSize();
                if (size.equals("720p")) {
                    rbDip720.setChecked(true);
                    frag_dip = 0;
                } else {
                    rbDip1280.setChecked(true);
                    frag_dip = 1;
                }
                final int frag_d = frag_dip;
                rgDip.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        if (checkedId == R.id.rb_camera_1) {
                            frag_dip = 0;
                        }
                        if (checkedId == R.id.rb_camera_2) {
                            frag_dip = 1;
                        }
                    }
                });
                dialogDip.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        if (frag_dip == 0) {
                            FacephonePreferences.instance().setPreferredVideoSize("720p");

                        } else if (frag_dip == 1) {
                            FacephonePreferences.instance().setPreferredVideoSize("1080p");

                        }
                        ciDip.setContentText(FacephonePreferences.instance().getPreferredVideoSize());
                        dialogDip.dismiss();
                    }
                });
                dialogDip.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        frag_dip = frag_d;
                        dialogDip.dismiss();
                    }
                });
                break;
            case R.id.ci_document:
                final CustomDialog dialogDocument = new CustomDialog(context);
                if (!getActivity().isFinishing()) {
                    dialogDocument.show();
                }
                View inflateDocument = View.inflate(context, R.layout.dialog_document, null);
                dialogDocument.setTitleText(R.string.help_document);
                dialogDocument.getContentView().addView(inflateDocument);
                dialogDocument.setOnCancelListener(new CustomDialog.OnCancelListener() {
                    @Override
                    public void onCancelClick() {
                        dialogDocument.dismiss();
                    }
                });
                dialogDocument.setOnSubmitListener(new CustomDialog.OnSubmitListener() {
                    @Override
                    public void onSubmitClick() {
                        dialogDocument.dismiss();
                    }
                });
                break;
        }

    }

    @Override
    public void onFocusChange(View view, boolean b) {
        MainActivity.currentMenuTime = 0;
        switch (view.getId()) {

            case R.id.ci_account:
            case R.id.ci_name:
            case R.id.ci_password:
            case R.id.ci_domain:
            case R.id.ci_proxy:
            case R.id.ci_protocol:
                scrollView.smoothScrollTo(0, 0);
                if (b) {
                    tvAccountSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_title));
                } else {
                    tvAccountSetting.setBackground(null);
                }
                break;

            case R.id.ci_answer:
            case R.id.ci_frame:
            case R.id.ci_stream:
            case R.id.ci_decode:
            case R.id.ci_local:
            case R.id.ci_language:
                scrollView.smoothScrollTo(0, ciFrame.getTop());
                if (b) {
                    tvVideoSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_title));
                } else {
                    tvVideoSetting.setBackground(null);
                }
                break;

            case R.id.ci_update:
            case R.id.ci_launch:
            case R.id.ci_screen:
            case R.id.ci_standby:
            case R.id.ci_document:
            case R.id.ci_dip:
                scrollView.smoothScrollTo(0, ciUpdate.getTop());
                if (b) {
                    tvLowSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_title));
                } else {
                    tvLowSetting.setBackground(null);
                }
                break;
            case R.id.tv_login:
            case R.id.tv_quit:
                break;
            case R.id.tv_account_setting:
                scrollView.smoothScrollTo(0, 0);
                tvAccountSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_bg));
                break;
            case R.id.tv_video_setting:
                scrollView.smoothScrollTo(0, ciFrame.getTop());
                tvVideoSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_bg));
                break;
            case R.id.tv_low_setting:
                scrollView.smoothScrollTo(0, ciUpdate.getTop());
                tvLowSetting.setBackground(context.getResources().getDrawable(R.drawable.setting_navi_bg));
                break;

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (FaceLogin.instance().getCurrentAuthInfo() != null) {
            ciAccount.setContentText(FaceLogin.instance().getCurrentAuthInfo().getUsername());
            ciDomain.setContentText(FaceLogin.instance().getCurrentAuthInfo().getDomain());
            ciProxy.setContentText(FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
        }
    }

    /**
     * 改变语言
     *
     * @param locale
     */
    private void switchLanguage(Locale locale) {
        Resources resources = context.getResources();// 获得res资源对象
        Configuration config = resources.getConfiguration();// 获得设置对象
        DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数：主要是分辨率，像素等。
        config.locale = locale; // 简体中文
        resources.updateConfiguration(config, dm);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            if (FaceLogin.instance().getCurrentAuthInfo() != null) {
                ciName.setContentText(FacephonePreferences.instance().getAccountDisplayName(FacephonePreferences.instance().getAccountCount() - 1));
                ciAccount.setContentText(FaceLogin.instance().getCurrentAuthInfo().getUsername());
                ciDomain.setContentText(FaceLogin.instance().getCurrentAuthInfo().getDomain());
                ciProxy.setContentText(FacephonePreferences.instance().getAccountProxy(FacephonePreferences.instance().getAccountCount() - 1));
            }
            if (context.getResources().getConfiguration().locale.toString().equals("zh_cn")) {
                ciLanguage.setContentText(("简体中文"));
            }
        }
    }

    //适配一体机鼠标点击事件，强制弹出软键盘
    private void popuSoft(EditText editText, int type) {
        editText.setInputType(type);
        editText.requestFocus();
        InputMethodManager immm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        immm.showSoftInput(editText, 0);
    }

    public boolean isIP(String addr, boolean hasPort) {
        if (addr.length() < 7 || "".equals(addr)) {
            return false;
        }
        /**
         * 判断IP格式和范围
         */
        String rexp;
        if (hasPort) {
            rexp = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)(:\\d+)$";
        } else {
            rexp = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        }

        Pattern pat = Pattern.compile(rexp);

        Matcher mat = pat.matcher(addr);

        boolean isAddress = mat.find();

        return isAddress;
    }


    public static Rect getOverScan() {
        Rect invoke = null;
        try {
            Class<?> classDisplayOutputManager = Class.forName(DISPLAY_OUTPUT_MANAGER);
            Method method = classDisplayOutputManager.getDeclaredMethod("getOverScan", int.class);
            Object displayOutputManager = classDisplayOutputManager.newInstance();
            invoke = (Rect) method.invoke(displayOutputManager, 0);
        } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {

        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return invoke;
    }

    /**
     * 设置一体机的屏幕缩放
     *
     * @param direction 方向 0-横向 1-纵向
     * @param value     缩放值
     * @param display
     */
    public static void setOverScan(int direction, int value, int display) {
        if (display < 0 || getDisplayNumber() == null || display > getDisplayNumber() - 1)
            return;

        try {
            Class<?> classDisplayOutputManager = Class.forName(DISPLAY_OUTPUT_MANAGER);
            Method method = classDisplayOutputManager.getDeclaredMethod("setOverScan", int.class, int.class, int.class);
            Object displayOutputManager = classDisplayOutputManager.newInstance();
            method.invoke(displayOutputManager, display, direction, value);

        } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
            Log.d("flag->", "DisplayUtils.java(setOverScan:90)->" + e.toString());
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }
    }

    public static Integer getDisplayNumber() {
        try {
            Class<?> classDisplayOutputManager = Class.forName(DISPLAY_OUTPUT_MANAGER);
            Method method = classDisplayOutputManager.getDeclaredMethod("getDisplayNumber");
            Object displayOutputManager = classDisplayOutputManager.newInstance();

            return (Integer) method.invoke(displayOutputManager);

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        }

        return null;
    }

}